/**
 * 
 */
package garin.parse;

import java.io.IOException;

public final class BlockComment extends CommentToken {
	private final boolean nest;
	private int nesting = 1;
	private boolean exit;
	private int ignore;
	private final String close = "*/";
	private final String open = "/*";
	
	public BlockComment(final Token source, final boolean nest) {
		super(source);
		this.nest = nest;
		ignore = open.length();
	}
	
	@Override
	public boolean accept() throws IOException {
		return source().is(open);
	}
	
	@Override
	public Token on() throws Exception {
		if (ignore > 0) {
			--ignore;
			return self();
		}
		if (exit) return up();
		if (nesting == 0) {
			exit = true;
			return self();
		}
		if (is(close) && size() > 1) --nesting;
		if (is(open) && nest) ++nesting;
		return self();
	}
}
